//
//  HQARStaticBuniness.m
//  DesignSDK
//
//  Created by lvfeijun on 2021/10/18.
//

#import "HQARStaticBuniness.h"
#import "NSString+Architecture.h"
#import "MJExtension.h"
#import "NSBundle+SDK.h"

@implementation HQARStaticBuniness

#pragma mark -- SpecialCharacters

+ (NSDictionary *)getSpecialCharacters
{
    NSString *filePath = [[NSBundle Ar_DesignSDKBundle] pathForResource:@"SpecialCharacters" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    return [data mj_JSONObject];
    return [NSDictionary dictionaryWithContentsOfFile:@"SpecialCharacters.json"];
}

+ (NSString *)dealSpecialCharacters:(NSString *)string
{
    if ([NSString hq_isEmptyString:string]) {
        return @"";
    } else if (string.length<4) {
        return string;
    }
    
    NSString *str = [NSString stringWithString:string];
    do {
        NSRange range1 = [str rangeOfString:@"<table"];
        if (range1.length) {
            NSRange range2 = [str rangeOfString:@"</table>"];
            NSRange range = NSMakeRange(range1.location, range2.location-range1.location+range2.length);
            str = [str stringByReplacingCharactersInRange:range withString:@""];
        }else{
            break;
        }
        if ([NSString hq_isEmptyString:str]) {
            return @"";
        }
    } while (str.length);
    
    str = [str hqar_replaceRegex:@"<img.*?(?:>|\/>" withString:@"[图片]"];
    
    str = [str hqar_replaceArrayRegex:@[@"<[^>]+>",@"<(.+?)[\s]*\/?[\s]*>",@"[\r\n]",@"<\/?.+?>",@"\s/",@""] withString:@""];
    
    NSDictionary *dic = [self getSpecialCharacters];
    NSArray *keys = dic.allKeys;
    for (int i=0; i<keys.count; i++) {
        NSString *key = keys[i];
        str = [str stringByReplacingOccurrencesOfString:key withString:dic[key]];
    }

    return str;
}

@end
